﻿using DevExpress.Mvvm;
using DevExpress.Mvvm.DataAnnotations;
using DevExpress.Mvvm.Native;
using DevExpress.Mvvm.UI;
using Serilog;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Input;
using WpfApp1.Models;

namespace WpfApp1
{
    public class MainViewModel: ViewModelBase
    {
        private ILogger logger;

        public MainViewModel(ILogger logger)
        {
            this.logger = logger; 
            MenuList = new ObservableCollection<MenuItemModel>(SimpleData.GetMenuItems());
            ColorList = new ObservableCollection<string>()
            {
                "Blue","Red","Pink","Dark","DarkGreen"
            };
            SelectedColor = Themes.PaletteHelper.ThemeType.ToString();

            DocumentViews = new ObservableCollection<PanelViewModel>();
        }

        public ObservableCollection<MenuItemModel> MenuList
        {
            get { return GetValue<ObservableCollection<MenuItemModel>>(); }
            set { SetValue(value); }
        }

        public object MainContent
        {
            get { return GetValue<object>(); }
            set { SetValue(value); }
        }
        
        public ObservableCollection<string> ColorList
        {
            get { return GetValue<ObservableCollection<string>>(); }
            set { SetValue(value); }
        }

        public string SelectedColor
        {
            get { return GetValue<string>(); }
            set { SetValue(value);
                Themes.PaletteHelper.ChangeTheme(value);         
            }
        }

        public ObservableCollection<PanelViewModel> DocumentViews
        {
            get { return GetValue<ObservableCollection<PanelViewModel>>(); }
            set { SetValue(value); }
        }

        public MenuItemModel MenuItem
        {
            get { return GetValue<MenuItemModel>(); } 
            set
            {
                SetValue(value);

                logger.Debug(MenuItem.Name);
                MainContent = ViewLocator.Default.ResolveView(MenuItem.View);

                DocumentViews.ForEach(x => x.IsActive = false);
                DocumentViews.Add(new DocumentViewModel() { Key = Guid.NewGuid().ToString(), Caption = MenuItem.View, Content = MainContent, IsActive = true, Glyph = "/Images/change.png" });
            }
        }
          
        [Command]
        public void ClosePanel(object obj)
        {
            if(obj is PanelViewModel panel)
            {
                var view = DocumentViews.FirstOrDefault(x => x.Key == panel.Key);
                DocumentViews.Remove(view);
            }
        } 
    } 
}
